package com.woniuxy.yoga.order.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.yoga.commons.alipay.util.AlipayUtil;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.util.JWTUtil;
import com.woniuxy.yoga.commons.redis.util.RedisUtil;
import com.woniuxy.yoga.commons.web.log.YogaLog;
import com.woniuxy.yoga.commons.web.util.WebUtil;
import com.woniuxy.yoga.order.model.dto.DataDto;
import com.woniuxy.yoga.order.model.dto.OrderInfoDto;
import com.woniuxy.yoga.order.model.form.*;
import com.woniuxy.yoga.order.model.param.*;
import com.woniuxy.yoga.order.permission.Permission;
import com.woniuxy.yoga.order.service.OrderInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@RestController
@RequestMapping("/orderInfo")
@Api(tags = "订单服务管理")
@Validated
public class OrderInfoController {

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 下单
     * @param form 下单参数
     * @param token token
     * @return OrderInfoDto
     */
    @PostMapping("/createOrder.us")
    @ApiOperation("下单")
    @YogaLog("下单")
    public Result<OrderInfoDto> createOrder(@Valid @RequestBody CreateOrderForm form, @RequestHeader String token){
        Integer userId = JWTUtil.getUserId(redisUtil.get(token));
        CreateOrderParam param = BeanUtil.toBean(form, CreateOrderParam.class);
        param.setUserId(userId);
        OrderInfoDto orderInfoDto = orderInfoService.createOrder(param);
        return Result.getResult(OrderInfoDto.class).setMessage("下单成功").setData(orderInfoDto);
    }

    /**
     * 私教课教练同意生成订单
     * @param form 私教课教练同意生成参数
     * @return OrderInfoDto
     */
    @PostMapping("/createPriOrder")
    @ApiOperation("私教课教练同意生成")
    @YogaLog("私教课教练同意生成")
    public Result<OrderInfoDto> createOrder(@Valid @RequestBody CreatePriOrderForm form){
        CreateOrderParam param = BeanUtil.toBean(form, CreateOrderParam.class);
        OrderInfoDto orderInfoDto = orderInfoService.createOrder(param);
        return Result.getResult(OrderInfoDto.class).setMessage("下单成功").setData(orderInfoDto);
    }

    /**
     * 支付订单
     * @param form 支付参数
     */
    @PostMapping("/pagePay.us")
    @ApiOperation("支付订单")
    @YogaLog("支付订单")
    public Result<String> pagePay(@RequestBody @Valid OrderPagePayForm form,@RequestHeader String authen_token) throws Exception {
        Integer userId = JWTUtil.getUserId(authen_token);
        OrderPagePayParam param = BeanUtil.toBean(form, OrderPagePayParam.class);
        param.setUserId(userId);
        String page = orderInfoService.pagePay(param);
        WebUtil.getResponse().setContentType("text/html;charset=utf8");
        WebUtil.getResponse().getWriter().write(page);
        WebUtil.getResponse().getWriter().flush();
        return Result.getResult(String.class).setMessage("响应成功").setData(page);
    }

    /**
     * 异步回调接口
     * @return String
     * @throws Exception Exception
     */
    @PostMapping("/orderNotify")
    @ApiOperation("异步回调接口")
    public String orderNotify() throws Exception {
        Map<String, String[]> parameterMap = WebUtil.getRequest().getParameterMap();
        Map<String, String> params = AlipayUtil.parseParams(parameterMap);
        orderInfoService.orderNotify(params);
        return "SUCCESS";
    }

    /**
     * 完成订单
     * @param orderNum 订单编号
     * @return String
     */
    @GetMapping("/completeOrder/{orderNum}")
    @ApiOperation("完成订单")
    @YogaLog("完成订单")
    public Result<String> completeOrder(@PathVariable("orderNum") @Valid @NotNull String orderNum) throws Exception {
        orderInfoService.completeOrder(orderNum);
        return Result.getResult(String.class).setMessage("操作成功");
    }

    /**
     * 分页查询所有订单
     * @param form 分页查询所有订单参数
     * @return PageInfo
     */
    @PostMapping("/selectOrders.api")
    @ApiOperation("分页查询所有订单")
    @YogaLog("分页查询所有订单")
    @Permission(resources = {"/order/orderInfo/selectOrders.api"})
    public Result<PageInfo> selectOrders(@RequestBody @Valid SelectOrdersForm form){
        PageInfo pageInfo = orderInfoService.selectOrders(BeanUtil.toBean(form, SelectOrdersParam.class));
        return Result.getResult(PageInfo.class).setMessage("查询成功").setData(pageInfo);
    }

    /**
     * 商家分页查看订单
     * @param form 商家分页查看订单参数
     * @param authen_token 令牌
     * @return 分页
     */
    @PostMapping("/selectStoreOrders.bu")
    @ApiOperation("商家分页查看订单")
    @YogaLog("商家分页查看订单")
    public Result<PageInfo> selectStoreOrders(@RequestBody @Valid SelectStoreOrdersForm form, @RequestHeader String authen_token){
        Integer businessId = JWTUtil.getBusinessId(authen_token);
        SelectStoreOrdersParam param = BeanUtil.toBean(form, SelectStoreOrdersParam.class);
        param.setBusinessId(businessId);
        PageInfo pageInfo = orderInfoService.selectStoreOrders(param);
        return Result.getResult(PageInfo.class).setMessage("查询成功").setData(pageInfo);
    }

    /**
     * 用户查询订单
     * @param form form
     * @param authen_token authen_token
     * @return PageInfo
     */
    @PostMapping("selectUserOrders.us")
    @ApiOperation("用户查询订单")
    @YogaLog("用户查询订单")
    public Result<PageInfo> selectUserOrders(@RequestBody @Valid SelectUserOrdersForm form, @RequestHeader String authen_token){
        Integer userId = JWTUtil.getUserId(authen_token);
        SelectUserOrdersParam param = BeanUtil.toBean(form, SelectUserOrdersParam.class);
        param.setUserId(userId);
        PageInfo pageInfo = orderInfoService.selectUserOrders(param);
        return Result.getResult(PageInfo.class).setMessage("查询成功").setData(pageInfo);
    }

    /**
     * 通过订单编号返回订单信息
     * @param orderNum 订单编号
     * @return OrderInfoDto
     */
    @GetMapping("/getByOrderNum/{orderNum}")
    @ApiOperation("通过订单编号返回订单信息")
    public Result<OrderInfoDto> getByOrderNum(@PathVariable("orderNum") String orderNum){
        OrderInfoDto orderInfoDto = orderInfoService.getByOrderNum(orderNum);
        return Result.getResult(OrderInfoDto.class).setMessage("查询成功").setData(orderInfoDto);
    }

    /**
     * 修改订单为已评价状态
     * @param form 参数
     */
    @PostMapping("/updateEvaluateState")
    @ApiOperation("修改订单评价状态")
    @YogaLog("修改订单评价状态")
    public Result<?> updateEvaluateState(@RequestBody @Valid UpdateEvaluateStateForm form){
        orderInfoService.updateEvaluateState(BeanUtil.toBean(form,UpdateEvaluateStateParam.class));
        return Result.getResult().setMessage("修改成功");
    }

    /**
     * 通过门店id查询订单集合
     * @param storeId 门店id
     * @return List<OrderInfoDto>
     */
    @GetMapping("/listOrdersByStoreId/{storeId}")
    @ApiOperation("通过门店id查询订单集合")
    @YogaLog("通过门店id查询订单集合")
    public Result<List<OrderInfoDto>> listOrdersByStoreId(@PathVariable @Valid @NotNull Integer storeId){
        List<OrderInfoDto> orderInfoDtos = orderInfoService.listOrdersByStoreId(storeId);
        return Result.getListResult(OrderInfoDto.class).setMessage("查询成功").setData(orderInfoDtos);
    }

    /**
     * 获取一定时间段场馆的订单成交量和营业额
     * @param form 参数
     * @return DataDto
     */
    @PostMapping("/getDataByTime")
    @ApiOperation("获取一定时间段场馆的订单成交量和营业额")
    @YogaLog("获取一定时间段场馆的订单成交量和营业额")
    public Result<DataDto> getDataByTime(@RequestBody GetDataForm form) {
        DataDto dataDto = orderInfoService.getDataByTime(BeanUtil.toBean(form, GetDataParam.class));
        return Result.getResult(DataDto.class).setMessage("获取成功").setData(dataDto);
    }

    /**
     * 分页查询在学学员信息
     * @return PageInfo
     */
    @PostMapping("/selectStudyUser.bu")
    @ApiOperation("商家分页查询在学学员信息")
    @YogaLog("商家分页查询在学学员信息")
    public Result<PageInfo> selectStudyUser(@RequestBody @Valid SelectStudyUserForm form,@RequestHeader String authen_token){
        Integer businessId = JWTUtil.getBusinessId(authen_token);
        SelectStudyUserParam param = BeanUtil.toBean(form, SelectStudyUserParam.class);
        param.setBusinessId(businessId);
        PageInfo pageInfo = orderInfoService.selectStudyUser(param);
        return Result.getResult(PageInfo.class).setMessage("查询成功").setData(pageInfo);
    }
}

